<template>
  <!-- 机器人详情 -->
  <el-dialog :visible.sync="isShow" v-if="isShow" title="机器人信息" width="50%" :before-close="handleClose" top="100px" :close-on-click-modal="false">
    <div class="border-box">
      <el-form ref="robotForm" :model="formParams" :rules="ruleForm" label-position="right" label-width="0">
        <div class="flex item-box" v-for="(item,index) in itemData" :key="index">
          <div class="title width25">
            {{ item.label }}<span :class="item.pass?'mustPass':'noMustPass'">*</span>
          </div>
          <div class="content width80">
            <el-form-item class="item-clear" v-show="item.key=='a_base_robot_id'" label="" prop="a_base_robot_id">
              <el-input-number :min="0" :max="999999999" :precision="0" v-model="formParams.a_base_robot_id" controls-position="right" clearable placeholder="请输入机器人ID" />
            </el-form-item>
            <el-form-item  class="item-clear" v-show="item.key=='a_robot_remark'" label="" prop="a_robot_remark">
              <el-input class="width40" type="text" v-model="formParams.a_robot_remark" clearable placeholder="请输入备注"></el-input>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='a_robot_type'" label="" prop="a_robot_type">
              <el-select v-model="formParams.a_robot_type" placeholder="请选择机器人类型" filterable >
                <el-option :label="item.label" :value="item.value" v-for="(item, index) in robotTypeData" :key="index"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='a_robot_money'" label="" prop="a_robot_money">
              <el-input-number :min="0" :max="999999999" :precision="0" v-model="formParams.a_robot_money" controls-position="right" clearable placeholder="请输入机器人初始筹码"  style="width:240px" />
            </el-form-item>
            <div v-show="item.key=='recharge'" class="paddingT20">
              <div class="flex" v-for="(second,two) in rechargeDataList" :key="two" style="height:50px">
                <div class="btn-box-add">
                  <el-button v-show="rechargeIndex==two" class="marginL10" size="mini" type="primary" circle icon="el-icon-plus" @click="addRecharge" />
                </div>
                <el-form-item label="次数:" label-width="70px">
                  <el-input-number :min="0" :max="999999999" :precision="0" v-model="second.item" controls-position="right" clearable placeholder="请输入充值次数"  />
                </el-form-item>
                <el-form-item label="概率:" label-width="60px">
                  <el-input-number :min="0" :max="10000" :precision="0" v-model="second.num" controls-position="right" clearable placeholder="请输入充值概率"  />
                </el-form-item>
                <div class="btn-box-delete">
                  <el-button v-show="rechargeDataList.length != 1" class="marginL10" size="mini" type="danger" circle icon="el-icon-minus" @click="deleteRecharge(index)" />
                </div>
              </div>
            </div>
            <el-form-item class="item-clear" v-show="item.key=='a_robot_vip'" label="" prop="a_robot_vip">
              <el-input-number :min="0" :max="999999999" :precision="0" v-model="formParams.a_robot_vip" controls-position="right" clearable placeholder="请输入机器人VIP等级"  style="width:200px" />
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='a_robot_weight'" label="" prop="a_robot_weight">
              <el-input-number :min="0" :max="10000" :precision="0" v-model="formParams.a_robot_weight" controls-position="right" clearable placeholder="请输入机器人被抽中权重"  style="width:200px" /><span class="colorRed">(万分比)</span>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="handleClose">取 消</el-button>
      <el-button size="medium" type="primary" @click="handleConfirm('robotForm')">确 认</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  addRobot,
  editRobot
} from "@/api/game";

export default {
  components: {},
  props: {
    show: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: function () {
        return {}
      }
    },
    robotTypeData: {
      type: Array,
      default: function () {
        return []
      }
    },
    robotType: {
      type: Number,
      default: 1
    },
  },
  data () {
    return {
      isShow: false,
      itemData:[
        { label: '机器人ID', key: 'a_base_robot_id' ,pass:true},
        { label: '备注', key: 'a_robot_remark' ,pass:true},
        { label: '机器人类型', key: 'a_robot_type' },
        { label: '初始筹码', key: 'a_robot_money' ,pass:true},
        { label: '机器人充值次数', key: 'recharge' },
        { label: '机器人VIP等级', key: 'a_robot_vip' },
        { label: '抽中权重', key: 'a_robot_weight' },
      ],
      formParams: {
        game_id:this.robotType,
        a_base_robot_id:'',
        a_robot_remark:'',
        a_robot_rechargetype:'',
        a_robot_vip:'',
        a_robot_money:0,
        a_robot_weight:'',
        a_robot_type:2,
      },
      ruleForm: {
        a_base_robot_id: [{ required: true, trigger: 'blur', message: '请输入机器人id', }],
        a_robot_remark: [{ required: true, trigger: 'blur', message: '请输入备注', }],
        a_robot_money: [{ required: true, trigger: 'blur', message: '请输入初始筹码携带', }],
      },
      rechargeDataList:[
        {item: 1, num: 1}
      ],
      rechargeIndex:0
    }
  },
  computed: {},
  watch: {
    show: function () {
      let that = this;
      that.isShow = that.show
      if (that.data) {
        that.formParams = JSON.parse(JSON.stringify(that.data))
        that.rechargeDataList=that.formParams.rechargetype
        that.rechargeIndex=that.rechargeDataList.length-1
      } else {
        that.formParams = {
          game_id:this.robotType,
          a_base_robot_id:'',
          a_robot_remark:'',
          a_robot_rechargetype:'',
          a_robot_vip:'',
          a_robot_money:0,
          a_robot_weight:'',
          a_robot_type:2,
        }
      }
    },
  },
  mounted () {},
  methods: {
    // 确认
    handleConfirm (formvalue) {
      let that = this,params = JSON.parse(JSON.stringify(that.formParams));
      params.a_robot_money=that.$public.mathNumberMultiply(params.a_robot_money,10000)
      params.a_robot_rechargetype=that.arrToStr(that.rechargeDataList)
      that.$refs[formvalue].validate((valid) => {
        if (valid) {
          if (!params.id) {
            addRobot(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          } else {
            editRobot(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          }
        } else {
          return false;
        }
      });
    },
    // 新增充值次数
    addRecharge () {
      this.rechargeIndex++
      this.rechargeDataList.push({
        item: 1, num: 1
      })
    },
    // 删除充值次数
    deleteRecharge (index) {
      this.rechargeIndex--
      this.rechargeDataList.splice(index, 1,)
    },
    arrToStr (arr) {
      let reward = '';
      if (arr[0] && typeof (arr[0]) == 'string') {
        reward = item.item + ',' + item.num + ';'
      } else {
        // 多个
        arr.map(item => {
          reward += item.item + ',' + item.num + ';'
        })
      }
      reward = reward.substring(0, reward.length - 1)
      return reward
    },
    // 取消
    handleClose () {
      this.$emit('onHideDetails', false)
    },
  }
}
</script>

<style scoped lang="scss">
.btn-box-add {
  width: 50px;
  margin-top: -20px;
  margin-right: -15px;
  text-align: center;
  .el-button {
    height: 28px;
  }
}
.btn-box-delete {
  width: 50px;
  margin-bottom: 20px;
  text-align: center;
  .el-button {
    height: 28px;
  }
}
</style>
